home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6162 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  70 lines

  1. Path: solon.com!not-for-mail
  2. From: kanze@gabi-soft.fr (J. Kanze)
  3. Newsgroups: comp.lang.c.moderated,comp.lang.c
  4. Subject: Re: Please help me elect rounding of int division
  5. Date: 22 Feb 1996 18:05:21 -0600
  6. Organization: GABI Software, Sarl.
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4gj0c1$li1@solutions.solon.com>
  10. References: <4gfaif$1tt@solutions.solon.com> <4ggbm9$85t@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4ggbm9$85t@solutions.solon.com> Gihan Perera
  14. <Gihan@andante.demon.co.uk> writes:
  15.  
  16. |> In article <4gfaif$1tt@solutions.solon.com>
  17. |>            swedecj@vcnet.com "Carl Jacobson" writes:
  18.  
  19. |> > Please help me solve this task.
  20. |> > 
  21. |> > I have now tried (Borland C++ version 3.0) for two solid days to write
  22. |> > a function that would allow me to round the quotient of (a/b) up or
  23. |> > down based on the "nearest" integer instead of being truncated to the
  24. |> > smallest. If exactly half way, return the "odd."
  25.  
  26. |> The first part is easy.  If you're using reals, add 0.5 to (a/b) before
  27. |> truncating it.  If you're doing the whole thing with integers, use:
  28.  
  29. |>    q = ( a + b/2 ) / b;
  30.  
  31. |> which has the same effect.
  32.  
  33. |> But neither of these meets your second criterion ("return the odd") -
  34. |> they both round _up_ if exactly half-way.  Sorry - I don't know any
  35. |> easy trick for that.
  36.  
  37. Both also fail when the results are negative.  For integer arithmetic
  38. involving only non-negative values, the following expression should do
  39. the trick:
  40.  
  41.     (a + b/2) / b - (b % 2 == 0 && a % b == b/2 && ((a/b) & 1) == 0)
  42.  
  43. To handle values no matter what sign, the only solution I can think of
  44. off hand is brute force:
  45.  
  46.     (a >= 0)   
  47.         ?    ((b >= 0)
  48.                 ?    (a + b/2) / b 
  49.                         - (b % 2 == 0 && a % b == b/2 && ((a/b) & 1) == 0)
  50.                 :    -((a + -b/2) / -b 
  51.                         - (-b % 2 == 0 && a % -b == -b/2 && ((a/-b) & 1) == 0))
  52.             )
  53.         :    ((b >= 0)
  54.                 ?    -((-a + b/2) / b 
  55.                         - (b % 2 == 0 && -a % b == b/2 && ((-a/b) & 1) == 0))
  56.                 :    (-a + -b/2) / -b 
  57.                         - (-b % 2 == 0 && -a % -b == -b/2 && ((-a/-b) & 1) == 0)
  58.             )
  59.  
  60. In production code, this may be pushing the expressive power of
  61. expressions a little too much for legibility:-).
  62.  
  63. The above is all off the top of my head.  No guarantee.  (And it
  64. obviously doesn't work if b == 0.  But then, what does?)
  65. -- 
  66. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  67. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  68. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  69.               -- A la recherche d'une activitΘ dans une region francophone
  70.